const Router = require('koa-router')
const { UserController, AuthcodeController, DefaultController } = require('../../controllers')
const { HttpDataResolve, Validator } = require('../../common')
const { authCode } = require('../../config')
const { Auth, SP_ADMIN, ADMIN } = require('../../middleware/auth')
const res = new HttpDataResolve()

const router = new Router({
  prefix: '/api/v1/user'
})

router.post('/', new Auth().t, async (ctx) => {
  const data = new Validator(ctx, {
    username: 'required|min:1|max:10',
    email: 'required|email',
    password: 'required|min:6|max:18',
    authCode: 'required|length:6'
  })

  await UserController.create(data);

  ctx.response.status = 200;
  ctx.body = res.success('注册成功');
})

router.post('/create/authcode', new Auth().t, async (ctx) => {
  const data = new Validator(ctx, {
    email: 'required|email'
  });

  const flag = await DefaultController.authcode(data, authCode.register_user, ctx, '注册用户');
  if(!flag) return;

  ctx.response.status = 200;
  ctx.body = res.success('邮件发送成功, 请查收');
})

router.post('/login', new Auth().t, async (ctx) => {
  const data = new Validator(ctx, {
    email: 'required|email',
    password: 'required|min:6|max:18'
  })

  let result = await UserController.login(data);

  ctx.response.status = 200;
  ctx.body = res.json(result, '登录成功');
})

router.get('/detail', new Auth().m, async (ctx) => {
  const user = await UserController.getInfo(null, ctx.auth);

  ctx.response.status = 200;
  ctx.body = res.json(user);
})

router.get('/detail/:uid', async (ctx) => {
  const data = new Validator(ctx.params, {
    uid: 'required'
  }, true)
  
  const user = await UserController.getInfo(data);

  ctx.response.status = 200;
  ctx.body = res.json(user);
})

router.post('/update', new Auth().m, async (ctx) => {
  const data = new Validator(ctx, {
    uid: 'required',
    information: '',
    github: '',
    web: '',
    company: '',
    cover: '',
    portrait: ''
  })

  await UserController.update(data, ctx);

  ctx.response.status = 200;
  ctx.body = res.success();
})

// SP_ADMIN把普通用户设置成管理员 
router.post('/scope', new Auth(SP_ADMIN).m, async (ctx) => {
  const data = new Validator(ctx, {
    uid: 'required',
    scope: 'required|include:USER,ADMIN,SP_ADMIN'
  })

  await UserController.setScope(data, ctx.auth);

  ctx.response.status = 200;
  ctx.body = res.success();
})

// 找回密码邮件
router.post('/password/authcode', new Auth().l, async ctx => {
  const data = new Validator(ctx, {
    email: 'email'
  })

  const flag = await DefaultController.authcode(
    data,
    authCode.update_password,
    ctx,
    '修改密码'
  )
  if (!flag) return

  ctx.response.status = 200
  ctx.body = res.success('邮件发送成功, 请查收')
})

router.post('/password/update', new Auth().l, async ctx => {
  const data = new Validator(ctx, {
    email: 'email',
    password: 'required|min:6|max:18',
    authCode: 'required|length:6'
  })

  const token = await UserController.updatePassword(data, ctx.auth)

  ctx.response.status = 200
  if(token){
    ctx.body = res.json({ token })
  } else {
    ctx.body = res.success();
  }
})

// router.post('/password/reset', async ctx => {
//   const data = new Validator(ctx, {
//     email: 'required|email',
//     password: 'required|min:6|max:18',
//     authCode: 'required|length:6'
//   })

//   await UserController.updatePassword(data, ctx)

//   ctx.response.status = 200
//   ctx.body = res.success()
// })

router.get('/list', new Auth().m, async ctx => {
  const data = new Validator(ctx.query, {
    page: 'number',
    size: 'number',
    order: 'include:DESC,ASC',
    keyword: '',
    id: ''
  }, true);
  const result = await UserController.list(data, ctx.auth);

  ctx.response.status = 200;
  ctx.body = res.json(result);
});

router.post('/logout', new Auth().m, async ctx => {
  await UserController.logout(ctx.auth);

  ctx.response.status = 200;
  ctx.body = res.success('登出成功');
})

router.get('/test', new Auth().m, async ctx => {
  const data = new Validator(ctx.query, {
    overtime: 'include:true'
  }, true)

  const token = await UserController.test(data, ctx.auth);

  ctx.response.status = 200;
  if(token){
    ctx.body = res.json({token}, '续时成功')
  } else {
    ctx.body = res.success()
  }
})

module.exports = router